        
    {% assign bing_list_id = storeConfig.bing_list | strtoarray %}
    {% get_pixels_domain list={bing_list_id} %}
    {%- assign pixels = list -%}
    {% if pixels %}

        <script>
    
            function UetqLoad(){
                this.uetq = [];
                this.pixels = {{pixels|json}};
    
    
                
    
            } 
            UetqLoad.prototype.initEvent = function(){
                {% if template == 'product_detail' %}
                    this.onEvent('view_item', {
                        'event_category': '{{product.product_type | filter_string_format}}',
                        'event_label': '{{ product.spu | escape}}',
                        'event_value': 1,
                        'ecomm_totalvalue': '{{ product.variant.price }}',
                        'revenue_value': '{{ product.variant.price }}',
                        'currency': '{{ base_currency.standard_code }}',
                        'ecomm_prodid': ["{{ product.spu | escape}}"],
                        'ecomm_pagetype': 'product',
                        'items': [
                            {
                            'id': '{{ product.spu | escape}}',
                            'category': '{{product.product_type | filter_string_format}}',
                            'quantity': 1,
                            'price': '{{ product.variant.price }}',
                            'variant': `{{product.variant.sku_value| replace: "<br />", ", " | strip_newlines}}`,
                            }
                        ]
                    });
                {% elsif template == 'search_detail' and search.keywords %}
                    var prodid = [];
                    {%- if search.products -%}
                    {%- for product in search.products -%}
                    prodid.push("{{ product.spu | escape}}");
                    {%- endfor -%}
                    {%- endif -%}
                    this.onEvent('search', {
                        'event_label': '{{search.keywords | filter_string_format}}',
                        'event_category': '{{template}}',
                        'ecomm_query': '{{search.keywords | filter_string_format}}',
                        'ecomm_prodid': prodid,
                        'ecomm_pagetype': 'searchresults',
                        'items': [
                        {%- for product in search.products -%}
                        {
                        'id': '{{ product.spu | escape}}',
                        'category': '{{product.product_type | filter_string_format}}',
                        'variant': `{{product.variant.sku_value | strip_newlines | replace: "<br />", ", " | escape | strip_newlines}}`,
                        'quantity': 1,
                        'price': '{{ product.variant.price}}',
                        },
                        {%- endfor -%}
                        ]
                    });
                {% elsif controller == 'index' and action == 'index' %}
                    this.onEvent('page_view_event', {'event_category': '{{template}}','ecomm_pagetype':'home'});
                {% elsif template == 'blog_detail' %}
                    this.onEvent('page_view_event',{
                        'event_category': '{{template}}',
                        'event_label': '{{blog.title | filter_string_format}}',
                        'event_value': '{{blog.id}}'
                    });
                {% elsif template == 'collection_detail' %}
                    var prodid = [];
                    {%- if collection.products -%}
                    {%- for product in collection.products -%}
                    prodid.push('{{ product.spu | escape}}');
                    {%- endfor -%}
                    {%- endif -%}
                    this.onEvent('view_item_list', {
                    'event_category': '{{template}}',
                    'event_label': '{{collection.title | filter_string_format}}',
                    'event_value': '{{collection.id}}',
                    'ecomm_category': '{{collection.title | filter_string_format}}',
                    'ecomm_prodid': prodid,
                    'ecomm_pagetype': 'category',
                    'items': [
                    {%- if collection.products -%}
                    {%- for product in collection.products -%}
                    {
                    'id': '{{ product.spu | escape}}',
                    'category': '{{product.product_type | filter_string_format}}',
                    'quantity': 1,
                    'price': '{{ product.variant.price}}',
                    'variant': `{{product.variant.sku_value| replace: "<br />", ", " | strip_newlines}}`,
                    },
                    {%- endfor -%}
                    {%- endif -%}
                    ]
                    });
    
                {% elsif template == 'page_detail' %}
                    this.onEvent('page_view_event',{
                    'event_category': '{{template}}',
                    'event_label': '{{page.title | filter_string_format}}',
                    'event_value': '{{page.id}}'
                    });
                    
                {% elsif template == 'order_success' %}
                        var spu = [];
                        {%- for product in order.products -%}
                        spu.push("{{ product.spu | escape}}");
                        {%- endfor -%}
                        this.onEvent('purchase', {
                        'ecomm_prodid': spu,
                        'ecomm_pagetype': 'purchase',
                        {%- if storeConfig.bing_sales_goods_amount -%}
                        'ecomm_totalvalue': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
                        'revenue_value': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
                        {%- else -%}
                        'ecomm_totalvalue': '{{ order.total_price }}',
                        'revenue_value': '{{ order.total_price }}',
                        {%- endif -%}
                        'currency': '{{ order.currency_code }}',
                        'transaction_id': '{{ order.order_number }}',
                        'items': [
                        {%- for product in order.products -%}
                            {
                            'id': '{{ product.spu | escape}}',
                            'category': '{{product.product_type | filter_string_format}}',
                            'quantity': '{{ product.quantity }}',
                            {%- if storeConfig.bing_sales_goods_amount -%}
                            'price': "{{ product.discount_price }}",
                            {%- else -%}
                            'price': "{{ product.price }}",
                            {%- endif -%}
                            'variant': '{{product.sku_value | replace: "<br />", ", " | filter_string_format}}',
                            },
                            {%- endfor -%}
                            ]
                        });
                {% elsif template == 'order_payment_gateway' and order.payment_type == 'offlinepay' and storeConfig.is_offlinepay_purchase_pixel == '1' %}
                        var spu = [];
                        {%- for product in order.products -%}
                        spu.push("{{ product.spu | escape}}");
                        {%- endfor -%}
                        this.onEvent('purchase', {
                        'ecomm_prodid': spu,
                        'ecomm_pagetype': 'purchase',
                        {%- if storeConfig.bing_sales_goods_amount -%}
                        'ecomm_totalvalue': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
                        'revenue_value': '{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price}}',
                        {%- else -%}
                        'ecomm_totalvalue': '{{ order.total_price }}',
                        'revenue_value': '{{ order.total_price }}',
                        {%- endif -%}
                        'currency': '{{ order.currency_code }}',
                        'transaction_id': '{{ order.order_number }}',
                        'items': [
                        {%- for product in order.products -%}
                            {
                            'id': '{{ product.spu | escape}}',
                            'category': '{{product.product_type | filter_string_format}}',
                            'quantity': '{{ product.quantity }}',
                            {%- if storeConfig.bing_sales_goods_amount -%}
                            'price': "{{ product.discount_price }}",
                            {%- else -%}
                            'price': "{{ product.price }}",
                            {%- endif -%}
                            'variant': '{{product.sku_value | replace: "<br />", ", " | filter_string_format}}',
                            },
                            {%- endfor -%}
                            ]
                        });
                {% else %}
                    this.onEvent('event', 'page_view_event',{
                        'event_category': '{{template}}'
                    });
                {% endif %}
            }
            
            UetqLoad.prototype.registerCallback = function(){
                const _that = this;
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('add_to_cart',function(ecdata){
                    _that.onEvent('add_to_cart', {
                        'event_category': ecdata['ec_data_category'],
                        'event_label': ecdata['ec_data_spu'],
                        'event_value': ecdata['ec_data_quantity'],
                        'ecomm_prodid': ecdata['ec_data_id'],
                        'ecomm_pagetype': 'cart',
                        'ecomm_totalvalue': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
                        'revenue_value': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
                        'currency': '{{ base_currency.standard_code }}',
                        'items': [{
                            'id': ecdata['ec_data_spu'],
                            'category': ecdata['ec_data_category'],
                            'quantity': ecdata['ec_data_quantity'],
                            'variant': ecdata['ec_data_sku_name'].replace('<br />',', '),
                            'price': ecdata['ec_data_price']
                        }]
                    });
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('generate_lead',function(){
                    _that.onEvent('generate_lead');
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('login',function(){
                    _that.onEvent('login');
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('begin_checkout',function(){
                    _that.onEvent('begin_checkout');
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('sign_up',function(){
                    _that.onEvent('sign_up');
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('share_product',function(ecdata, platform){
                    _that.onEvent('share_product', {
                        'event_category': ecdata['ec_data_category'],
                        'event_label': ecdata['ec_data_spu'],
                        'event_value': ecdata['ec_data_quantity'],
                        'revenue_value': ecdata['ec_data_price'],
                        'currency': "{{ base_currency.standard_code }}",
                    });
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('remove_from_cart',function(ecdata, platform){
                    _that.onEvent('remove_from_cart', {
                        'event_category': ecdata['ec_data_category'],
                        'event_label': ecdata['ec_data_spu'],
                        'event_value': ecdata['ec_data_quantity'],
                        'revenue_value': ecdata['ec_data_price']*ecdata['ec_data_quantity'],
                        'currency': "{{ base_currency.standard_code }}"
                    });
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('add_address_information',function(ecdata, platform){
                    _that.onEvent( 'checkout_progress',{'event_category': 'add_address_information'});
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('add_shipping_information',function(ecdata, platform){
                    _that.onEvent('checkout_progress',{'event_category': 'add_shipping_information'});
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('add_payment_information',function(ecdata, platform){
                    _that.onEvent('add_payment_information');
                    _that.onEvent('checkout_progress',{'event_category': 'add_payment_information'});
                })
                window['TOP_GLOBAL_CALLBACK_LAYER'].add('add_to_wishlist',function(ecdata, platform){
                    _that.onEvent('add_to_wishlist', {
                        'event_category': ecdata['ec_data_category'],
                        'revenue_value': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
                        'currency': "{{ base_currency.standard_code }}"
                    });
                })
            }
    
          
            UetqLoad.prototype.init = function(){
                this.pixels.forEach(t=>{
                    var e = t.facebook_pixel_id;
                    this.uetq[e] = new UET({
                        ti: e,
                        q: []
                    }),
                    this.uetq[e].push("pageLoad")
                })
                this.initEvent()
                this.registerCallback()
            }
    
            UetqLoad.prototype.onEvent = function(name,data){
                this.pixels.forEach(t=>{
                    var e = t.facebook_pixel_id;
                    this.uetq[e].push("event",name,data)
                })
            }
        
            window['uetq_load'] = new UetqLoad();
    
    
            (function (w, d, t, r) {
                var f, n, i;
                n = d.createElement(t), n.src = r, n.async = 1, n.onload = n.onreadystatechange = function () {
                    var s = this.readyState;
                    s && s !== 'loaded' && s !== 'complete' || (uetq_load.init(), n.onload = n.onreadystatechange = null)
                }, i = d.getElementsByTagName(t)[0], i.parentNode.insertBefore(n, i)
            })(window, document, 'script', '//bat.bing.com/bat.js', 'uetq');
        </script>
    {% endif %}
